﻿--[[
	Notebook-like User Control Panel
	A simple, nice and useful control panel.
	(c) by Dzakub 2011
	
	Feel free to edit, modify and change this resource whatever you want.
	
	Icons system:
	Basic info:
	The best gap between two icons is 70 px horizontally and 60 px vertically
	
	desktops - table with desktop's IDs where icon should exist
	x - defines X coordinate of icon. WARNING: Relative to 1st pixel of notebook image
	y - defines Y coordinate of icon. WARNING: Relative to 1st pixel of notebook image
	img - path to the icon !!! BE FUCKING SURE TO ADD IMAGE TO META.XML (<file></file>) !!!
	onclick - function triggered when player click on the icon *1
	label - label below the icon
	

	
	Events:
	All of those events can be cancelled.
	
	onClientPlayerOpenNotebook - Fired when player request notebook open. No arguments.
	onClientPlayerCloseNotebook - Fired when player request notebook close. No arguments.
	onClientPlayerClickIcon - Fired when player click icon. One argument - table with icon info
	onClientPlayerSetActiveDekstop - Fired when change of active desktop is requested. Argument 1 - old desktop ; Argument 2 - new desktop
	
	Functions (exported) :
	setPlayerActiveDesktop(int desktop) - request a desktop change to 'desktop' argument

	-------------------------------------
	
	*1 - Put your handler function below
	
	
	TODO:
		* Ability to change wallpaper.
		* 2 FUTURE DO : Ability to move iconz [lfsworld.net]
	
]]


icons = { -- this crap is explained above.
	["cp"] = {desktops={0},
			  x=130,
			  y=60,
			  img="icons/cp.png",
			  onclick=false, -- changeme
			  label="Panel\n Sterowania"},
	["pm"] = {desktops={0},
	          x=200,
			  y=60,
			  img="icons/pm.png",
			  onclick=handlePM, -- changeme
			  label="Komunikator"},
}
--[[
desktops = { -- 4 FUTURE USE.
	[0] = {		 -- ROOT DESKTOP. DO NOT TOUCH. NEVER.
			background = "images/notebook.png"
			},
	[1] = {}
}]]

scrX, scrY = guiGetScreenSize ()
g_Show = false
x = scrX/2 - 944/2
y = scrY - 636
segoeUI = dxCreateFont( "fonts/segoeui.ttf", 8 )
setElementData(localPlayer, "activeDesktop", 0)
-- outputDebugString ( "Loaded icon's list. Number of icons (all desktops): "..tostring(#icons) ) -- not working :(

-- EVENTS --

addEvent("onClientPlayerOpenNotebook", true)
addEvent("onClientPlayerCloseNotebook", true)
addEvent("onClientPlayerClickIcon", true)
addEvent("onClientPlayerSetActiveDekstop", true)

-- End of events --

function initIcons() -- create icons elements if they don't exist
	for k,v in pairs(icons) do -- go through every icon
		if v.element == nil then -- if icon does not exists
			local img = guiCreateStaticImage ( x+v['x'],y+v['y'], 55, 55, v.img, false ) -- create it's image
			guiSetVisible(img, false) -- hide it
			v.element = img -- 'attach' it to it's record in table
			v.clickHandler = function() -- create handler function (which actually calls function given in onclick key). Pretty convinient, isn't it?
				if triggerEvent ( "onClientPlayerClickIcon", localPlayer, k ) then -- look below
					pcall(v.onclick) -- pcall (f, arg1, ...) - Calls function f with the given arguments in protected mode [...]
				end
			end
			local r = addEventHandler("onClientGUIDoubleClick", v.element, v.clickHandler, false)
		end
	end
end
initIcons() -- will execute on script start

addEventHandler("onClientRender", getRootElement(), function()
    if g_Show then
         dxDrawImage(x,y,944,636,"images/notebook.png")
		 for k,v in pairs(icons) do
			--dxDrawImage(x+v['x'],y+v['y'],55,55,v['img'])
			dxDrawText ( v['label'], x+v['x']+56, y+v['y']+56, x+v['x'], y+v['y']+32, tocolor(0,0,0), 1, segoeUI, "center")
			dxDrawText ( v['label'], x+v['x']+55, y+v['y']+55, x+v['x'], y+v['y']+32, tocolor(255,255,255), 1, segoeUI, "center") -- , x+v['x'], y+v['y']+32, int color=white, float scale=1, mixed font="default", string alignX="left", string alignY="top", bool clip=false, bool wordBreak=false, bool postGUI ] )
		 end
    end 
end)

function openHandler(key, keyState) -- shows/hides notebook & icons
--	VIP	--
	if getElementData(localPlayer, "vipon") == true then
--	VIP	--
	local activeDesktop = getElementData(localPlayer, "activeDesktop")
	if g_Show then -- if notebook is up
		if triggerEvent ( "onClientPlayerCloseNotebook", localPlayer ) then -- that IF handles event cancelling. If result of triggerEvent is false = event got cancelled
			for k,v in pairs(icons) do -- go through every icon
				guiSetVisible(v.element, false) -- and hide it
			end
			g_Show = false
			showCursor(false)
		end
	else -- if not
		if triggerEvent ( "onClientPlayerOpenNotebook", localPlayer ) then -- look above :)
			for k,v in pairs(icons) do -- go through every icon
				for kp, vp in pairs(v.desktops) do -- go through every desktops table
					if activeDesktop == vp then -- check if it should be shown or not
						guiSetVisible(v.element, true)
					end
				end
			end
			showCursor(true)
			g_Show = true
		end
	end
-- VIP	--
	end
-- VIP	--
end

bindKey ( "N", "down", openHandler )

function setPlayerActiveDesktop(desktop)
	local activeDesktop = getElementData(localPlayer, "activeDesktop")
	if desktop == activeDesktop then -- we are checking if desktop that got requested equals active player's desktop
		outputDebugString ( "Player's active desktop == requested desktop. Halting." )
	else -- success, we can change active desktop
		if triggerEvent ( "onClientPlayerSetActiveDekstop", localPlayer, activeDesktop, dekstop ) then -- look above :)		
			outputDebugString ( "Changing player's active dekstop from "..tostring(activeDesktop).." to "..tostring(dekstop) )
			setElementData(localPlayer, "activeDesktop", tonumber(dekstop))
			if g_Show then
				openHandler()
			end
		end
	end
end